<?php
/**
 * @copyright: ©2024 永州 微聚 商 网络科技有限公司
 * @link: https://www.weiJuyunke.com
 */
namespace app\api\controller\consume;
use app\admin\model\CouponOrder;
use app\common\controller\AdminController;
use think\App;

/**
 * @ControllerAnnotation(title="会员管理")
 */
class Consumer extends AdminController
{
    
    use \app\admin\traits\Curd;

    /**
     * 字段排序
     *
     * @var array
     */
    protected $sort = [
        'create_time' => 'desc'
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        
        $this->model = new \app\admin\model\Consumer();
    }
    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        $request = $this->request;
        $param = $request->param();
        $page = input("page");
        $limit = input("limit");
        $where[] = ['type','=',1];
        $where[] = ['is_delete','=',0];



        if ($this->request->platform_id >0 ){
            $where[] = ['platform_id','=',$this->request->platform_id];
        }
        if (isset( $param['name']) &&   $param['name']){
            $where[] = ['name','like',"%{$param['name']}%"];
        }

        if (isset( $param['issue_type']) &&   $param['issue_type'] ){
            $where[] = ['issue_type','=',$param['issue_type']];
        }

        if (isset( $param['platform_name']) &&   $param['platform_name'] ){
            $where[] = ['platformInfo.name','=',$param['platform_name']];
        }
        $count = $this->model->withJoin([
                'platformInfo',

            ], 'LEFT')
            ->where($where)
            ->count();
        $list = $this->model->withJoin([

            'platformInfo'
        ], 'LEFT')
            ->where($where)
            ->page($page, $limit)
            ->order($this->sort)
            ->select();

        foreach ($list as  $key => $value){
            if ($value['expire_time']){
                $list[$key]['expire_time']  = date('Y-m-d H:i',$value['expire_time']);
            }else{
                $list[$key]['expire_time']  = '无时效';
            }

        }
        return msg([
            'list' => $list,
            'count' => $count
        ], 0, "success");
    }
    public function add(){
        $request = $this->request;
        $param = $request->param();
        $StoreConsumerModel  =  new \app\admin\model\Consumer();
        if (empty($param['expire_time'])){
            return  msg([], 1, "请选择有效时间") ;
        }

        if (empty($param['issue_type'])){
            return  msg([], 1, "请选择消费券类型") ;
        }

        if ($param['issue_type'] == 1){
            if (empty($param['deductible_amount'])){
                return  msg([], 1, "请输入抵扣金额") ;
            }
            if (empty($param['satisfy_amount'])){
                return  msg([], 1, "请输入满足条件金额") ;
            }
        }elseif ($param['issue_type'] == 1){
            if (empty($param['deductible_amount'])){
                return  msg([], 1, "请输入折扣金额") ;
            }
            if (empty($param['satisfy_amount'])){
                return  msg([], 1, "请输入满足条件金额") ;
            }
        }else{
            if (empty($param['title'])){
            return  msg([], 1, "请输入实物券名称") ;
            }
            if (empty($param['goods_img'])){
                return  msg([], 1, "请上传实物券图片") ;
            }

        }
        $StoreConsumerData = [
            'deductible_amount' => isset($param['deductible_amount']) ?$param['deductible_amount']:0 ,
            'satisfy_amount' =>    isset($param['satisfy_amount']) ?$param['satisfy_amount']:0 ,
            'title' =>    isset($param['title']) ?$param['title']:0 ,
            'issue_type' =>    isset($param['issue_type']) ?$param['issue_type']:0 ,
            'goods_img' =>    isset($param['goods_img']) ?$param['goods_img']:'' ,
            'consumer_type' =>  $param['consumer_type'],
            'expire_time' => strtotime($param['expire_time']),
            'type' => 1,
            'status' => 1,
            'platform_id'=> $this->request->platform_id,
            'store_undertake_proportion' => isset($param['store_undertake_proportion'])?$param['store_undertake_proportion']:0,
            'price' => isset($param['price'])?$param['price']:0,

        ];
        if (isset($param['id']) && $param['id']){
            $StoreConsumerModel->where(['id' => $param['id']])->update($StoreConsumerData);
        }else{
            $StoreConsumerModel->save($StoreConsumerData);
        }

        return msg([], 0, "添加成功!");
    }
    public function edit(){
        $request = $this->request;
        $param = $request->param();
        $StoreConsumerModel  =  new \app\admin\model\Consumer();
        $StoreConsumerData = [
            'deductible_amount' => $param['deductible_amount'],
            'satisfy_amount' => $param['satisfy_amount'],
            'consumer_type' =>  $param['consumer_type'],
            'expire_time' => strtotime($param['expire_time']),
            'type' => 1,
            'platform_id'=> $this->request->platform_id,
            'store_undertake_proportion' => isset($param['store_undertake_proportion'])?$param['store_undertake_proportion']:0,
            'price' => isset($param['price'])?$param['price']:0,
            'issue_type' =>    isset($param['issue_type']) ?$param['issue_type']:0 ,
            'goods_img' =>    isset($param['goods_img']) ?$param['goods_img']:'' ,

        ];
        if (isset($param['id']) && $param['id']){
            $StoreConsumerModel->where(['id' => $param['id']])->update($StoreConsumerData);
        }else{
            $StoreConsumerModel->save($StoreConsumerData);
        }

        return msg([], 0, "修改成功!");
    }
    public function delete(){
        $request = $this->request;
        $param = $request->param();
        $StoreConsumerModel  =  new \app\admin\model\Consumer();
        $this->model->whereIn('id', $param['id'])->update(['is_delete' => 1]);

        return msg([], 0, "删除成功!");
    }
    public function consumerList(){
        $request = $this->request;
        $param = $request->param();
        $page = input("page");
        $limit = input("limit");
        $where[] = ['type','=',2,];
        $where[] = ['consumer.is_delete','=',0];
      //  $where[] = ['consumer.expire_time','>',time()];
        if ($this->request->platform_id >0 ){
            $where[] = ['consumer.platform_id','=',$this->request->platform_id];
        }
        if (isset( $param['name']) &&   $param['name']){
            $where[] = ['name','like',"%{$param['name']}%"];
        }
        if (isset( $param['platform_name']) &&   $param['platform_name'] ){
            $where[] = ['platformInfo.name','=',$param['platform_name']];
        }
        if (isset( $param['store_telphone']) &&   $param['store_telphone'] ){
            $where[] = ['storeInfo.telphone|storeInfo.store_name','like',"%".$param['store_telphone']."%"];
        }
        if (isset( $param['telphone']) &&   $param['telphone'] ){
            $where[] = ['storeInfo.telphone','=',$param['telphone']];
        }
        $count = $this->model->withJoin([
            'platformInfo',
            'storeInfo'
        ], 'LEFT')
            ->where($where)
            ->count();

        $list = $this->model->withJoin([
            'storeInfo',
            'platformInfo'
        ], 'LEFT')
            ->where($where)
            ->page($page, $limit)
            ->order($this->sort)
            ->select();
        foreach ($list as  $key => $value){
            if ($value['expire_time']){
                $list[$key]['expire_time']  = $value['expire_time'].'天';
            }else{
                $list[$key]['expire_time']  = '无时效';
            }

        }
        return msg([
            'list' => $list,
            'count' => $count
        ], 0, "success");
    }

    public function consumerAll(){
        $request = $this->request;
        $param = $request->param();
        $where[] = ['type','=',2];
        $where[] = ['consumer.is_delete','=',0];
        $where[] = ['consumer.expire_time','>',time()];
        if ($this->request->platform_id >0 ){
            $where[] = ['consumer.platform_id','=',$this->request->platform_id];
        }
        $list = $this->model->withJoin([
            'storeInfo',
            'platformInfo'
        ], 'LEFT')
            ->where($where)

            ->order($this->sort)
            ->select();

        ;

        foreach ($list as $key =>$value){
            if ($value['issue_type'] == 1){
                $list[$key]['label'] = $value['storeInfo']['store_name'] . '---满'.$value['satisfy_amount'] . '减'.$value['deductible_amount'];

            }elseif ($value['issue_type'] == 2){
                $list[$key]['label'] = $value['storeInfo']['store_name'] . '---满'.$value['satisfy_amount'] . '抵扣'.$value['deductible_amount'].'%';

            }else{
                $list[$key]['label'] = $value['storeInfo']['store_name'] . $value['title'];

            }

        }
        return msg([
            'list' => $list,
        ], 0, "success");
    }


    public function indexList()
    {
        $request = $this->request;
        $param = $request->param();
        $page = input("page");
        $limit = input("limit");

        $where[] = ['type','=',1];
        $where[] = ['consumer.is_delete','=',0];
        $where[] = ['issue_type','=',1];
        $where[] = ['consumer.expire_time','>',time()];
        if ($this->request->platform_id >0 ){
            $where[] = ['platform_id','=',$this->request->platform_id];
        }

        if (isset( $param['name']) &&   $param['name']){
            $where[] = ['name','like',"%{$param['name']}%"];
        }
//
//        if (isset( $param['issue_type']) &&   $param['issue_type'] ){
//            $where[] = ['issue_type','in',[1,2]];
//        }

        if (isset( $param['platform_name']) &&   $param['platform_name'] ){
            $where[] = ['platformInfo.name','=',$param['platform_name']];
        }
        $count = $this->model->withJoin([
            'platformInfo',

        ], 'LEFT')
            ->where($where)
            ->count();
        $list = $this->model->withJoin([

            'platformInfo'
        ], 'LEFT')
            ->where($where)
            ->page($page, $limit)
            ->order($this->sort)
            ->select();

        foreach ($list as  $key => $value){
            $list[$key]['expire_time']  = date('Y-m-d H:i',$value['expire_time']);
        }
        return msg([
            'list' => $list,
            'count' => $count
        ], 0, "success");
    }




}